import config from "@/constants/config"
import HttpRequest from "./HttpRequest"
import useUserStore from "@/store/useUserStore"
import $$ from "@/utils/wzUtils"
export const $http = new HttpRequest({
    baseURL: config.baseUrl, interceptorHooks: {
        requestInterceptor: request => {
            // 为请求头添加token
            // const token = "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjczY2ZiYmY2LWNjYmEtNDE4ZC1hNGQyLWRkZmQzZTk4YjUyNSJ9.SuJTOZIIKI_Jw0IGTXPCYvZA-YTGrZUXrON6U5DzDXt3m_zwgCeF6FSwhsKVBggXuxH_FmlWH4rp_o6xbLUUfw"
            const userStore = useUserStore()
            console.log("userStore.userToken===", userStore);

            //@ts-ignore
            if (userStore.userToken) request.headers["Authorization"] = userStore.userToken
            return request;
        },
        responseInterceptor: (response) => {
            if (response.data.code === 200) return response
            // 登录失效
            if ([401, 402,].includes(response.data.code)) {
                setTimeout(() => {
                }, 500);
                $$.showToast("登陆已失效，请重新登陆", "warning")
                return response
            }
            $$.showToast(response.data.msg || "服务器异常，请稍后再试", "error")
            return response
        }
    }
})